<template>
  <div class="app-container" style="width: 400px;">
    <el-form ref="ruleForm" v-loading="loading" :model="ruleForm" :rules="rules" label-width="120px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="ruleForm.name" />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input v-model="ruleForm.idCard" />
      </el-form-item>
      <el-form-item label="是否有不良记录" prop="proprietor">
        <el-select v-model="ruleForm.isLock" placeholder="请选择…" @change="selectGet">
          <el-option :label="'是'" :value="true"></el-option>
          <el-option :label="'否'" :value="false"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="请选择添加日期" prop="addTime">
        <el-date-picker v-model="ruleForm.addTime" type="datetime" placeholder="选择日期时间"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { queryAccountList } from '@/api/account'
import { addBadRecord } from '@/api/badRecord'

export default {
  name: 'BadRecordAdd',
  data() {
    return {
      ruleForm: {
        name: '',
        idCard: '',
        isLock: '',
        addTime:'',
      },
      accountList: [],
      rules: {
        name: [
          { required: true, trigger: 'blur' }
        ],
        idCard: [
          { required: true, trigger: 'blur' }
        ],
        isLock: [
          { required: true, message: '请选择不良记录', trigger: 'change' }
        ],
        addTime: [
          { required: true, message: '请选择日期', trigger: 'change' }
        ],
      },
      loading: false,
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', date);
          }
        }]
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm('是否立即创建?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'success'
          }).then((action) => {
            console.log('Action:', action);
            if (action === 'confirm') {
              this.loading = true;
              addBadRecord({
                name: this.ruleForm.name,
                id_card: this.ruleForm.idCard,
                is_lock: `${this.ruleForm.isLock}`,
                add_time: this.ruleForm.addTime,
              }).then(response => {
                this.loading = false;
                if (response !== null) {
                  this.$message({
                    type: 'success',
                    message: '创建成功!'
                  });
                } else {
                  this.$message({
                    type: 'error',
                    message: '创建失败!'
                  });
                }
              }).catch(error => {
                console.error('Error:', error);
                this.loading = false;
              });
            } else {
              console.log('Action is not confirm');
              console.log('创建取消');
            }
          }).catch(() => {
            console.log('Action is fail');
            console.log('创建取消');
          });
        } else {
          return false;
        }
      });
    },
    selectGet(isLock) {
      this.ruleForm.isLock = isLock
    }
  }
}
</script>

<style scoped>
</style>
